const app = getApp()
import api from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //等待框
    waitHidden: true,
    feedbackList: [], // 问题与反馈列表
    pageSize: 20, // 页面数量
    currentPage: 1,
    screenHeight: 0,
    isLastPage:true,
    mainScrollViewHeight: 1624
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.hideShareMenu()
    //设置屏幕尺寸数据
    that.setData({
      windowHeight: app.globalData.windowHeight,
      screenHeight: app.globalData.screenHeight
    })
    that.getFileList()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    that.setData({
      currentPage: that.data.currentPage + 1,
      waitHidden: false
    })
    if (!that.data.isLastPage) {
      that.getFileList()
    }
    that.setData({
      waitHidden: true
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取文件名字列表
   */
  getFileList() {
    var that = this
    var list = [{
        filename: "rtretra.doc",
        updateTime: "2022-1-12 12:00",
        fileId: 1
      }, {
        filename: "b.doc",
        updateTime: "2022-1-12 12:00",
        fileId: 2
      },
      {
        filename: "trhytkkc.doc",
        updateTime: "2022-1-12 12:00",
        fileId: 3
      }, {
        filename: "fafd.doc",
        updateTime: "2022-1-12 12:00",
        fileId: 4
      }, {
        filename: "dfade.doc",
        updateTime: "2022-1-12 12:00",
        fileId: 5
      },
      {
        filename: "dfaff.doc",
        updateTime: "2022-1-12 12:00",
        fileId: 6
      }, {
        filename: "dafdafdsfg.doc",
        fileId: 7
      }, {
        filename: "43rexv h.doc",
        fileId: 8
      },
      {
        filename: "try978po';/dafai.doc",
        fileId: 9
      }, {
        filename: "/uo;.revfvd0j.doc",
        fileId: 10
      }, {
        filename: "mw                 k.doc",
        fileId: 11
      },
      {
        filename: "dgf44444444444a.doc",
        fileId: 12
      }, {
        filename: "666666666666a.doc",
        fileId: 13
      }, {
        filename: "[[[[[[[[[[[[[a.doc",
        fileId: 14
      },
      {
        filename: "a.doc",
        fileId: 15
      }, {
        filename: "e.doc",
        fileId: 16
      }, {
        filename: "g.doc",
        fileId: 17
      }, {
        filename: "h.doc",
        fileId: 18
      },
      {
        filename: "i.doc",
        fileId: 19
      }, {
        filename: "j.doc",
        fileId: 20
      },
    ]

    // that.setData({
    //   feedbackList: list,
    //   // isLastPage: !res.data.data.totalRecords || res.data.data.currentPage == res.data.data.totalPages,
    //   waitHidden: true,
    //   // mainScrollViewHeight: that.data.mainScrollViewHeight + heightAdd
    // })
    wx.request({
      url: api.getFileList,
      method: "POST",
      data: {
        pageSize: that.data.pageSize,
        currentPage: that.data.currentPage
      },
      success(res) {
        if (res.data.data.totalRecords != 0) {
          for (var i = 0; i < res.data.data.records.length; i++) {
            var filename = res.data.data.records[i].fileName
            var updateTime = res.data.data.records[i].updateTime
            var fileId = res.data.data.records[i].id
            var tmp = {
              filename,
              updateTime,
              fileId
            }
            that.data.feedbackList.push(tmp)
          }
          that.setData({
            feedbackList: that.data.feedbackList,
            isLastPage: res.data.data.pageIndex >= res.data.data.totalPages,
            waitHidden: true,
            // mainScrollViewHeight: that.data.mainScrollViewHeight + heightAdd
          })
        }

        wx.createSelectorQuery().select('#item0').boundingClientRect().exec((res) => {
          if (res[0].height >= 812) {
            that.setData({
              height: res[0].height + 50
            })
          } else {
            that.setData({
              height: 812
            })
          }
        })
      }
    })
  },

  /**
   * 打开文件
   */
  openFile: function (e) {
    var that = this
    var curFileName = e.currentTarget.dataset.current
    
    that.setData({
      waitHidden: false
    })
    wx.request({
      url: api.openFile,
      method: "POST",
      data: {
        fileName: curFileName
      },
      responseType: 'arraybuffer', //此处是请求文件流，必须带入的属性
      success: res => {
        if (res.statusCode === 200) {
          const fs = wx.getFileSystemManager(); //获取全局唯一的文件管理器
          fs.writeFile({
            filePath: wx.env.USER_DATA_PATH + "/" + curFileName, // wx.env.USER_DATA_PATH 指定临时文件存入的路径
            data: res.data,
            encoding: "binary", //二进制流文件必须是 binary
            success(res2) {
              wx.openDocument({ // 打开文档
                filePath: wx.env.USER_DATA_PATH + "/" + curFileName, //拿上面存入的文件路径
                showMenu: true, // 显示右上角菜单
                success: function (res3) {
                  setTimeout(() => {
                    wx.hideLoading()
                    that.setData({
                      waitHidden: true
                    })
                  }, 500)
                }
              })
            }
          })
        } else {
          that.setData({
            waitHidden: true
          })
          wx.showToast({
            title: '文件打开失败',
            icon: 'error'
          })
        }
      }
    })
  }
})